
import BufferTool from "./BufferTool.js"
import Message from "../class/Message.js"


export default class MessageTool {

  /**
   * 测试
   * @param {Message} message
   * @param {Object} headerStruc
   */
  static writeHeaders(message, headerStruc) {
    
    var totalLength = 0;
    for (var i in headerStruc) {
      var key = headerStruc[i][0];
      var dataLength = headerStruc[i][1];
      var data = headerStruc[i][2] || 0;

      message.headerStruc[key] = BufferTool.dataToBuffer(data, dataLength)
      message.headerStrucFormat[key] = BufferTool.littleToBigEndian(message.headerStruc[key]).toString('hex')
      totalLength += dataLength;
    }

    message.headerBuffer = Buffer.allocUnsafe(totalLength);

    let offset = 0
    for (var i in headerStruc) {
      var key = headerStruc[i][0];
      var dataLength = headerStruc[i][1];
      var data = headerStruc[i][2] || 0;

      message.headerStruc[key].copy(message.headerBuffer, offset, 0)
      offset += dataLength
    }
    
    message.headerOffset = totalLength
  }

  static writeBody(connectObj, message, bodyStruc) {

    let totalLength = 0;
    let dataMap = {}
    // 构建 dataMap key与值的映射
    for (var i in bodyStruc) {
      var key = bodyStruc[i][0];
      var dataLength = bodyStruc[i][1] || 1;
      var defaultValue = bodyStruc[i][2] || 0;



      message.bodyStruc[key] = BufferTool.dataToBuffer(message.bodyStruc[key] || defaultValue, dataLength, connectObj) ; //message.bodyStruc[key]可能会在处理其他字段的时候就被设置了，比如下面的长度字段。

      if (typeof dataLength === 'string') {
        /** 
          length 为文字，数据长度直接取值的length
          ['SecurityBufferLength', 2],
          ['Buffer', 'SecurityBufferLength'],
        */
        let lengthName = dataLength
        dataLength = message.bodyStruc[key].length;
        message.bodyStruc[lengthName] = dataLength; //设置对应的长度字段
      } 
      totalLength += dataLength
    }
    message.bodyBuffer = Buffer.allocUnsafe(totalLength);

    // 写入 bodyBuffer
    let offset = 0
    for (var i in bodyStruc) {
      var key = bodyStruc[i][0];
      var dataLength = bodyStruc[i][1] || 1;
      message.bodyStruc[key] = BufferTool.dataToBuffer(message.bodyStruc[key], dataLength, connectObj); //上面可能有长度字段被重新赋值了，但是没转成buffer


      message.bodyStrucFormat[key] = BufferTool.littleToBigEndian(message.bodyStruc[key]).toString('hex')
      message.bodyStruc[key].copy(message.bodyBuffer, offset, 0)
      offset += dataLength
    }
    message.bodyOffset = totalLength
  }


  static readHeaders(message, headerStruc, buffer) {
   
    var offset = 0;
    let dataMap = {}
    for (var i in headerStruc) {
      var key = headerStruc[i][0];
      var length = headerStruc[i][1];
      dataMap[key] = buffer.slice(offset, offset + length);

      message.headerStrucFormat[key] = BufferTool.littleToBigEndian(dataMap[key]).toString('hex')
      offset += length;
    }

    message.headerOffset = offset
    message.headerStruc = dataMap


  }

  static readBody(message, bodyStruc, buffer) {
    let dataMap = {}
    let offset = 0
    for (var i in bodyStruc) {
      var key = bodyStruc[i][0];
      var dataLength = bodyStruc[i][1] || 1;
      if (typeof dataLength === 'string') {
        dataLength = BufferTool.bufferToNumber(dataMap[dataLength]);
      }
      dataMap[key] = buffer.slice(offset, offset + dataLength);
      message.bodyStrucFormat[key] = BufferTool.littleToBigEndian(dataMap[key]).toString('hex')
      offset += dataLength;
    }
    message.bodyOffset = offset
    message.bodyStruc = dataMap

  }

}